using System;
using Microsoft.SPOT;
using System.IO;
using System.Collections;
using System.Xml;

namespace Dairy_Town
{
    public static class ConfigurationManager
    {
        private const string APPSETTINGS_SECTION = "appSettings";
        private const string ADD = "add";
        private const string KEY = "key";
        private const string VALUE = "value";

        private static Hashtable appSettings;

        static ConfigurationManager()
        {
            appSettings = new Hashtable();
        }

        public static string GetAppSetting(string key)
        {
            return GetAppSetting(key, null);
        }

        public static string GetAppSetting(string key, string defaultValue)
        {
            if (!appSettings.Contains(key))
                return defaultValue;
            return (string)appSettings[key];
        }

        public static void Load(Stream xmlStream)
        {
            using (XmlReader reader = XmlReader.Create(xmlStream))
            {
                while (reader.Read())
                {
                    switch (reader.Name)
                    {
                        case APPSETTINGS_SECTION:
                            while (reader.Read())
                            {
                                if (reader.Name == APPSETTINGS_SECTION)
                                    break;

                                if (reader.Name == ADD)
                                {
                                    var key = reader.GetAttribute(KEY);
                                    var value = reader.GetAttribute(VALUE);

                                    //Debug.Print(key + "=" + value);
                                    appSettings.Add(key, value);
                                }
                            }

                            break;
                    }
                }
            }
        }
    }
}
